﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyMainSite.Models
{
    public class ProjectsIndexModel : BaseModel
    {
        #region Public Methods

        public ProjectsIndexModel(string name)
        {
            if (String.IsNullOrEmpty(name))
                return;

            this.Create(name);
        }

        #endregion

        #region Private Methods

        private void Create(string name)
        {
            RichardsSiteEntities dc = new RichardsSiteEntities();

            var proj = from p in dc.Projects
                       where p.FriendlyUrl.ToLower().Trim() == name.ToLower().Trim()
                       select p;

            if (proj.Count() != 1)
                throw new InvalidOperationException(String.Format("'{0}' is an invalid project name.",
                    name));

            var project = proj.Single();
            this.CurrentProject = project.FriendlyUrl;
            this.BreadCrumb.Add(new KeyValuePair<string, string>(project.Name, project.FriendlyUrl));
        }

        #endregion
    }
}